package leetcode.offer;

/**
 * ClassName: MaxSubArray
 * Description:leetcode-剑指offer 42.连续子数组的最大和
 * date: 2021-11-04 10:01
 *
 * @author liyifan
 */
public class MaxSubArray {
    public int maxSubArray(int[] nums){
        if (nums.length == 0)
            return 0;
        if (nums.length == 1)
            return nums[1];
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        int max = dp[0];
        for(int i = 1;i < nums.length;i++){
            dp[i] = Math.max(dp[i - 1] + nums[i],nums[i]);
            max = Math.max(max,dp[i]);
        }
        return max;
    }
}
